package com.globallogic.vkulyk.SSHTool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jcraft.jsch.*;

public class FilePutter extends FileCopier {

	private String srcFile = "";
	private String dstDir = "";
	private final Logger logger = LoggerFactory
			.getLogger(FilePutter.class);

	public FilePutter(String hostArg, int portArg, String userArg,
			String passwordArg, String srcFileArg, String dstDirArg) {
		super(hostArg, portArg, userArg, passwordArg);
		srcFile = srcFileArg;
		dstDir = dstDirArg;
	}

	public void execute() {
		Session session = openSession();
		executeInExistingSession(session);
		closeSession(session);
	}
	
	@Override
	public void executeInExistingSession(Session sessionArg) {
		Session session = sessionArg;
		ChannelSftp channelSFTP = openSFTPChannel(session);
		logger.info("Putting {} -> {} ", srcFile, dstDir);
		try {
			channelSFTP.put(srcFile, dstDir);
			logger.info("Finished put operation.");
		} catch (SftpException e) {
			logger.error(e.getMessage(), e);
		}
		closeSFTPChannel(channelSFTP);
	}

}
